package com.xy.shop.upload.controller;

import com.xy.shop.common.constant.AppConstants;
import com.xy.shop.common.utils.DateUtil;
import com.xy.shop.upload.util.UploadMessage;
import com.xy.shop.web.service.BaseParamService;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

/**
 * @author zyy
 */
@Controller
public class UploadController {

    @Resource
    private BaseParamService baseParamService;

    static final  String FILENAME="upload";

    /**
    *@Description: 文件存放在linux /wwwroot/java/lewan/ +upload 目录下
    *@Param:  null
    *@Author: zyy
    *@date: 2020/7/13
    *@Time: 14:18
    */
    @ResponseBody
    @RequestMapping(value="/img/fileUp.action")
    public UploadMessage upload(HttpServletResponse response, HttpServletRequest request) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Headers", "Authentication");
        UploadMessage uploadMessage=new UploadMessage();
        response.setCharacterEncoding("utf-8");
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        // 文件保存路径 ctxPath本地路径
        String ctxPath = baseParamService.selectByPrimarycode(AppConstants.BaseParam.IMG_PATH_ROOT);
        String realPath = baseParamService.selectByPrimarycode(AppConstants.BaseParam.IMG_VISIT_PATH);
        ctxPath += File.separator;
        String addFile = DateUtil.getCurrentDayStr().replace("-", "");
        ctxPath += "/"+FILENAME+"/"+addFile + "/";
        realPath+="/"+FILENAME+"/";

        System.out.println("ctxpath=" + ctxPath);
        // 创建文件夹
        File file = new File(ctxPath);
        System.out.println("save imge path=="+ctxPath);
        System.out.println("save imge file boolean=="+file.exists());
        if (!file.exists()) {
            boolean b=file.mkdirs();
            System.out.println("create file boolean=="+b);
        }

        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
            // 上传文件
            MultipartFile mf = entity.getValue();
            //获取原文件名
            String realNmae =UUID.randomUUID().toString().replace("-","")+ mf.getOriginalFilename();
            File uploadFile = new File(ctxPath + realNmae);
            try {
                FileCopyUtils.copy(mf.getBytes(), uploadFile);
                System.out.println("path=" + ctxPath + realNmae);
                //Runtime.getRuntime().exec("chmod 775 -R " + ctxPath  + "/" + realNmae);
                uploadMessage.setState(200);
                uploadMessage.setUrl(realPath  + "/"+addFile+ "/" + realNmae);
                uploadMessage.setMessage("success");
                System.out.println("上传成功");
            } catch (IOException e) {
                System.out.println("上传失败");
                e.printStackTrace();
            }
        }
        return uploadMessage;

    }
}
